/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import java.util.Random;

import ruter.map.Geometry;

/**
 *
 * @author CITI INSA-Lyon
 */
public class GaussMarkovRandomPosition {

	private final double JUMPING_DISTANCE;
	private final double MAX_ANGLE;
	private final double ALPHA;

	private Random random;
	public double x, y;
	private double mx, my;

	public GaussMarkovRandomPosition(double jumpingDistance, double alpha, double maxAngle) {
		random = new Random(Double.doubleToLongBits(Math.random()));
		JUMPING_DISTANCE = jumpingDistance;
		MAX_ANGLE = maxAngle;
		ALPHA = alpha;
	}

	public void generateRandomMove(double myPosX, double myPosY, double actualAngle) {
		double distance = Geometry.distance(myPosX, myPosY, x, y);
		if(!checkModified() || distance < JUMPING_DISTANCE) {
			double nextAngle = ALPHA*Geometry.checkAngle(actualAngle) + (1-ALPHA)*MAX_ANGLE + Math.sqrt(1-Math.pow(ALPHA, 2))*random.nextGaussian();
			x += JUMPING_DISTANCE*Math.cos(nextAngle);
			y += JUMPING_DISTANCE*Math.sin(nextAngle);
			mx = x;
			my = y;
		}
	}

	private boolean checkModified() {
		return x == mx && y == my;
	}
}
